/*
 * 组件注释
 * @Author: machinist_wq
 * @Date: 2021-04-027 16:15:06
 * @LastEditors: machinist_wq
 * @LastEditTime: 2022-07-01 15:12:12
 * @Description: 这里定义 全局的公共方法
 * 人生无常！大肠包小肠......
 */
"use strict";

export const getQuery = location => {
  var url = location.search; // 获取url中"?"符后的字串
  var theRequest = {};
  if (url.indexOf("?") !== -1) {
    var strs = url.substr(1).split("&");
    for (var i = 0, len = strs.length; i < len; i++) {
      theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
    }
  }
  return theRequest;
};
export function getChildren(list, pid) {
  return list
    .filter(item => item.pid === pid)
    .map(item => {
      return {
        id: item.funcId,
        name: item.funcName,
        url: item.url,
        type:
          item.urlType === 1
            ? "router"
            : item.urlType === 2
            ? "_blank"
            : "building",
        children: getChildren(list, item.funcId),
      };
    });
}

/*
 *
 * @data 需要查询的数据
 * 结构为带有chaildren的数组
 * @id 需要获取的那个字段的值
 * @idName 想要根据那个字段查询
 * @searchValue 想要的是哪个字段
 * */
export function getfieldName(
  data,
  id,
  idName = "menuId",
  searchValue = "menuName"
) {
  let value = "";
  (function loop(_data) {
    return _data.some(item => {
      if (item[idName] === id) {
        value = item[searchValue];
        return true;
      }
      if (item.children) {
        return loop(item.children);
      }
      return false;
    });
  })(data);

  return value;
}

/**
 *
 * @param data 数据
 * @param key 要查询的key
 * @param callback ck
 * @param keys 比对的字段名
 * @returns
 */
const loop = (data, key, callback, keys = "key", children = "children") =>
  data.some((item, index) => {
    if (item[keys] === key) {
      callback(item, index, data);
      return true;
    }
    if (item[children]?.length) {
      return loop(item[children], key, callback, keys, children);
    }
    return false;
  });
/**
 * 表单公共样式
 */
const layoutStyles = {
  labelCol: {
    span: 4,
  },
  wrapperCol: {
    span: 20,
  },
};
/*
 * 树形结构
 * @list 原数据
 * idField pidField 映射关系
 * */
function makeTree(list, idField, pidField) {
  let map = {};
  list.forEach(e => {
    map[e[idField]] = e;
  });

  let roots = [];
  list.forEach(e => {
    let pid = e[pidField];
    let p = map[pid];
    if (p) {
      if (!p.children) {
        p.children = [];
      }

      const oldE = p.children.find(item => e[idField] === item[idField]);
      if (!oldE) {
        p.children.push(e);
      }
    } else {
      roots.push(e);
    }
  });
  return roots;
}
function makeTreeData(list, idField, pidField) {
  let map = {};
  list.forEach(e => {
    map[e[idField]] = e;
  });

  let roots = [],
    parents = [];
  list.forEach(e => {
    let pid = e[pidField];
    let p = map[pid];
    if (p) {
      if (!p.children) {
        p.children = [];
      }
      p.children.push(e);
      parents.push(p);
    } else {
      roots.push(e);
    }
  });
  return { dataSource: roots, parents };
}
const percentageConversion = str => {
  let data;
  if (typeof str === "string") {
    data = Number(str);
  } else {
    data = str;
  }
  return Math.floor(data * 10000) / 100 + "%";
};
window.getfieldName = getfieldName;
window.loop = loop;
window.layoutStyles = layoutStyles;
window.MakeTree = makeTree;
window.makeTreeData = makeTreeData;
window.percentageConversion = percentageConversion;
